<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>个人信息</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <img src="avatar.jpg" style="width:200px">
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ user.yhxm }}</div>
      </div>
    </div>

    <div class="user-bio">
      <div class="user-education user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="education" /><span>个人信息</span></div>
        <div class="user-bio-section-body">
          <el-row>
            <el-col :span="6"><div class="grid-content bg-purple">姓名：</div></el-col>
            <el-col :span="12"><div class="grid-content bg-purple-light">{{ user.yhxm }}</div></el-col>
          </el-row>
          <el-row>
            <el-col :span="6"><div class="grid-content bg-purple">所属公司：</div></el-col>
            <el-col :span="12"><div class="grid-content bg-purple-light">{{ user.yhgs }}</div></el-col>
          </el-row>
          <el-row>
            <el-col :span="6"><div class="grid-content bg-purple">角色：</div></el-col>
            <el-col :span="12"><div class="grid-content bg-purple-light">{{ user.role }}</div></el-col>
          </el-row>
          <el-row>
            <el-col :span="6"><div class="grid-content bg-purple">用户组：</div></el-col>
            <el-col :span="12"><div class="grid-content bg-purple-light">{{ user.yhgroup }}</div></el-col>
          </el-row>
        </div>
      </div>

      <div class="user-skills user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="skill" /><span>操作</span></div>
        <div class="user-bio-section-body">
          <div class="progress-item">
            <el-button type="primary" size="mini" @click="show_modal()">
              修改密码
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="原密码" prop="old_pswd">
          <el-input v-model="temp.old_pswd" type="password" />
        </el-form-item>
        <el-form-item label="新密码" prop="new_pswd">
          <el-input v-model="temp.new_pswd" type="password" />
        </el-form-item>
        <el-form-item label="再次输入" prop="re_pswd">
          <el-input v-model="temp.re_pswd" type="password" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="resetPswd()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { resetPswd } from '@/api/user'
export default {
  components: { },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          username: '',
          email: '',
          //avatar: '@/assert/user/user.png',
          role: '',
          area_name: ''
        }
      }
    }
  },
  data() {
    return {
      dialogFormVisible: false,

      temp: {
        old_pswd: '',
        new_pswd: '',
        re_pswd: ''
      },
      rules: {
        old_pswd: [{ required: true, message: '原密码不能为空', trigger: 'change' }],
        new_pswd: [{ required: true, message: '新密码不能为空', trigger: 'change' }],
        re_pswd: [{ required: true, message: '重复密码不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    show_modal() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    resetPswd() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.temp.re_pswd !== this.temp.new_pswd) {
            this.$notify({
              title: '错误',
              message: '两次输入密码不一致',
              type: 'error',
              duration: 3000
            })
            return false
          }
          const tempData = Object.assign({}, this.temp)
          delete tempData.re_pswd
          resetPswd(tempData).then((res) => {
            this.dialogFormVisible = false
            this.$notify({
              title: '操作成功',
              message: '修改密码成功',
              type: 'success',
              duration: 3000
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>